package router

import (
	"encoding/json"
	"io/ioutil"
	"net/http"

	"github.com/lierdan/golangweb/internal/dbops"
	"github.com/lierdan/golangweb/internal/defs"

	resp "github.com/lierdan/golangweb/internal/response"

	"github.com/julienschmidt/httprouter"
)

func AddUser(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	//获取用户 和 密码
	bytes, _ := ioutil.ReadAll(r.Body)
	user := &defs.User{}

	err := json.Unmarshal(bytes, user)
	if err != nil {
		resp.SendErrorResponse(w, defs.ErrorRequestBodyParseFailed)
		return
	}

	// 注册到数据库
	err = dbops.RegisterUser(user)
	if err != nil {
		resp.SendErrorResponse(w, defs.ErrorRequestBodyParseFailed)
		return
	}

	resp.SendNormalResponse(w, "success", http.StatusBadRequest)
}
